//公告
$('#notice').live('pageshow', function(e, data){
    if($.cachePage(e.target, data.prevPage))  return true;
    var loginName = student.getLoginName();
    var noticelist = [];
    var current = 0;
    
    function show_notice(index){
        if(!noticelist[index]) return false;
        current = parseInt(index);
        $('#notice_show').databind(noticelist[index]);
        $.mobile.changePage('#notice_show');
        return true;
    }
    //获取本地存储的信息
    function localNotice(){
        if(!localStorage) return false;
        var localNotice = localStorage.localNotice
        if(localNotice) localNotice = JSON.parse(localNotice);
        noticelist = (localNotice && localNotice.length>0) ? localNotice : [];
        return true;
    }
    //将新获取的信息添加到本地存储
    function appendNotice(data){
        if(!localNotice()) {
            noticelist = data;
            return true;
        }
        if(data.length>0){
            noticelist = data.concat(noticelist);
            localStorage.localNotice = JSON.stringify(noticelist);
            setSyncDate();
        }
        return true;
    }
    //设置最后更新时间
    function setSyncDate(){
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!

        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+'-'+mm+'-'+dd;
        localStorage.noticeSyncDate = today;
    }
    //获取最后更新时间
    function getSyncDate(){
        return localStorage.noticeSyncDate ? localStorage.noticeSyncDate : '';
    }
    var adp = new $.listadapter(function(obj, data){
        var tpl = '<li><a href="#">${TITLE}</a></li>';
        var list = '';
        //将新数据合并到旧数据
        appendNotice(data)
        for(var i in noticelist){
            list += '<li><a href="#'+i+'">'+noticelist[i]['TITLE']+'</a></li>';
        }
        obj.html(list).listview('refresh', true);
        obj.find('a').click(function(){
            show_notice($(this).attr('href').substr(1))
        })
    });
    var pdv = new $.dataprovider('data.php?a=NOTICE&loginName='+loginName+"&syncDate="+getSyncDate());
    $(this).find('[data-role=listview]').bindlist(adp, pdv);
    
    $('.navbtn').find('a').off('click');
    $('.navbtn').find('a').eq(0).click(function(){
        show_notice(current-1)
    })
    $('.navbtn a').eq(1).click(function(){
        show_notice(current+1)
    })
})